<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/comm/mytags.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="后台管理系统">
    <link rel="shortcut icon" href="${ctx}/static/img/favicon.ico">
    <link rel="stylesheet" href="${ctx}/static/layui_v2/css/layui.css">
    <script type="text/javascript" src="${ctx}/static/js/jquery-1.8.3.js" ></script>
    <script src="${ctx}/static/layui_v2/layui.js"></script>

</head>
<body class="childrenBody" style="font-size: 12px;margin: 10px 10px 0;">
<form class="layui-form layui-form-pane">
    <input id="prizeId" name="prizeId" type="hidden" value="${prize.prizeId}">

    <input id="pageFlag"  type="hidden" value="${pageFlag}">
    <div class="layui-form-item">
        <label class="layui-form-label">奖品名</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="prizeName" lay-verify="required|prizeName" maxlength="20" value="${prize.prizeName}" placeholder="请输入奖品名">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">奖品剩余数量</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="prizeCount" lay-verify="required|prizeCount" maxlength="11" value="${prize.prizeCount}" placeholder="请输入奖品剩余数量">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">奖品总数</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="prizeTotalCount" lay-verify="required|prizeTotalCount" maxlength="10" value="${prize.prizeTotalCount}" placeholder="请输入奖品总数">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所需积分</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="credits" lay-verify="required|credits" maxlength="10" value="${prize.credits}" placeholder="请输入积分">
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">奖品状态</label>
        <div class="layui-input-block">
            <c:if test="${pageFlag == 'addPage' }">
                <input type="radio" name="status" value="1" title="有效" checked >
                <input type="radio" name="status" value="0" title="失效" >
            </c:if>
            <c:if test="${pageFlag == 'updatePage' and  prize.status == '0' }">
                <input type="radio" name="status" value="1" title="有效"  disabled <c:if test="${prize.status ==1 }">checked</c:if>/>
                <input type="radio" name="status" value="0" title="失效" disabled  <c:if test="${prize.status ==0 }">checked</c:if>/>
            </c:if>
            <c:if test="${pageFlag == 'updatePage' and  prize.status == '1' }">
                <input type="radio" name="status" value="1" title="有效"   <c:if test="${prize.status ==1 }">checked</c:if>/>
                <input type="radio" name="status" value="0" title="失效"   <c:if test="${prize.status ==0 }">checked</c:if>/>
            </c:if>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1" style="width: 150px;height: 150px;">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <c:if test="${pageFlag == 'addPage' }">
                <img class="test"  name="prizeImg" id="uploadImg" style="width: 150px;height: 150px;display: none;"  />
            </c:if>
            <c:if test="${pageFlag == 'updatePage'  }">
                <img class="test"  name="prizeImg" id="uploadImg" style="width: 150px;height: 150px;" src="${prize.prizeImg}"/>
            </c:if>
            <input type="hidden" name="prizeImg" id="prizeImg" value="${prize.prizeImg}" />
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
            <button class="layui-btn" lay-submit="" lay-filter="saveUser">保存</button>
            <button type="layui-btn" id="cancle" class="layui-btn layui-btn-primary">取消</button>
    </div>
</form>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '${ctx}/upload/' //上传接口
            ,done: function(res){
                $("#uploadImg").attr("src", res.msg).show();
                $("#prizeImg").attr("value", res.msg);
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>
<script type="text/javascript">
    layui.config({
        base : "${ctx}/static/js/"
    }).use(['form','layer','jquery','common'],function(){
        var $ = layui.$,
                form = layui.form,
                common = layui.common,
                layer = parent.layer === undefined ? layui.layer : parent.layer;

        /**表单验证*/
        form.verify({
            prizeName: function(value, item){
                //验证用户名
                if(value.trim().length > 20 || value.trim().length < 2){
                    return "奖品名必须2到20个字符之间";
                }
            },
            prizeTotalCount:function (value, item) {
                if(isNaN(value)){
                    return "请输入数字";
                }
            },
            credits:function (value, item) {
                if(isNaN(value)){
                    return "请输入数字";
                }
            },
            prizeCount:function (value, item) {
                if(isNaN(value)){
                    return "请输入数字";
                }
            },
        });

        /**保存*/
        form.on("submit(saveUser)",function(data){
            var pageFlag = $("#pageFlag").val();
            var userSaveLoading = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            //登陆验证
            $.ajax({
                url : '${ctx}/savePrize',
                type : 'post',
                async: false,
                data : data.field,
                success : function(data) {
                    if(data.code == 0){
                        top.layer.close(userSaveLoading);
                        common.cmsLaySucMsg("保存成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭                        //刷新父页面
                        parent.location.reload();
                    }else{
                        top.layer.close(userSaveLoading);
                        common.cmsLayErrorMsg(data.msg);
                    }
                },error:function(data){
                    top.layer.close(index);
                }
            });
            return false;
        });
        //取消
        $("#cancle").click(function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

    });

</script>
</body>
</html>